package com.njegovanobradovic.pages.admin;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.njegovanobradovic.entities.Page;
import com.njegovanobradovic.so.Save;

public class EditPage {
	@Persist
	private Page page;
	@Inject
	private Save<Page> savePage;
	@Property
	private String pageName;
	@Property
	private String title;
	@Property
	private String description;
	@Property
	private String content;
	@Property
	private int orderNum;
	@Property
	private boolean slideshow;
	@Property
	private boolean aside;
	@Property
	private boolean contactForm;
	@Property
	private boolean active;
	
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	
	public void onActivate(){
		try {
			pageName = page.getPageName();
			title = page.getTitle();
			description = page.getDescription();
			content = page.getContent();
			orderNum = page.getOrderNum();
			slideshow = page.isSlideshow();
			aside = page.isAside();
			active = page.isActive();
			contactForm = page.isContactForm();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	
	@Component
    private Form editPageForm;
	
	@CommitAfter
	public Object onSubmitFromEditPageForm(){
		try {
			page.setActive(active);
			page.setAside(aside);
			page.setContent(content);
			page.setDescription(description);
			page.setOrderNum(orderNum);
			page.setPageName(pageName);
			page.setSlideshow(slideshow);
			page.setTitle(title);
			page.setContactForm(contactForm);
			savePage.setObject(page);
			savePage.execute();
			return AllPages.class;
		} catch (Exception e) {
			System.out.println("\n\n\n*********************************\nError:\n"+e);
			return null;
		}
	}
	
}
